/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Golf_Manager.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 *
 * @author Administrator
 */
public class Util {
    
    
    /*입력한 날짜기 유효한 날짜인지 체크*/
    public static boolean checkDate(String year, String month, String day) {
        try {
            
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
            Date result = formatter.parse(year + "." + month + "." + day);
            String resultStr = formatter.format(result);
            
            return resultStr.equalsIgnoreCase(year + "." + month + "." + day);        
        } catch (Exception e) {
            return false;
        }
    }
    
    public static boolean isStringNumber(String s) {
        
        try {
            Integer.parseInt(s);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    
    public static String getAgebyBirthDate(String birth){
        String age = "";
        if(birth != null && birth.trim().length() == 10){
            age = String.valueOf(Calendar.getInstance().get(Calendar.YEAR) - (Integer.parseInt(birth.substring(0, 4)) - 1));
        }
        return age;
    }
    
    public static List<Integer> getRandomRange(int min, int max) {
        List<Integer> indexList = new ArrayList<>();
        List<Integer> randomindexList = new ArrayList<>();
        for(int index = min; index < max; index++){
            indexList.add(index);
        }
        
        Random random = new Random();
        
        for(int i = indexList.size(); i > 0; i--){
            int index = random.nextInt(i);      //현재 list 길이 만큼의 숫자에서 랜덤 숫자 뽑아냄
            randomindexList.add((Integer)indexList.get(index)); //랜덤 값이 나온 값을 리스트에서 뽑아냄 (결국 랜덤으로 나온 값을 뽑아내는것과 같음
            indexList.remove(index);       //뽑아낸 리스트 제거           
        }
        
        return randomindexList;
    }
}
